package org.redmagic.agent.location.web;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.redmagic.Request;
import org.redmagic.Response;
import org.redmagic.TransactionManager;
import org.redmagic.location.UriLocation;
import org.redmagic.location.web.JsonRedMagicRequest;
import org.redmagic.utils.Conditions;

public class WebLocation extends UriLocation {

    private final List<List<String>> hierarchy;
    private final TransactionManager transactionManager;

    public WebLocation(String uri, List<List<String>> hierarchy, TransactionManager transactionManager) {
        super(uri);
        this.hierarchy = new ArrayList<List<String>>(hierarchy);
        this.transactionManager = Conditions.notNull(transactionManager, "transactionManager");
    }

    public WebLocation(URI uri, List<List<String>> hierarchy, TransactionManager transactionManager) {
        super(uri);
        this.hierarchy = new ArrayList<List<String>>(hierarchy);
        this.transactionManager = Conditions.notNull(transactionManager, "transactionManager");
    }

    @Override
    public Properties resolvePlaceholders(Set<String> unresolvedPlaceholders) {
        Request request = new JsonRedMagicRequest(hierarchy, unresolvedPlaceholders);
        Response response = transactionManager.execute(this.getURI(), request);
        return response.getProperties();
    }
}